/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample;

import me.panpf.sketch.Configuration;
import me.panpf.sketch.Initializer;
import me.panpf.sketch.SLog;
import me.panpf.sketch.Sketch;
import me.panpf.sketch.sample.event.Events;
import me.panpf.sketch.sample.util.XpkIconUriModel;
import me.panpf.sketch.util.Log;
import ohos.app.Context;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * SampleSketchInitializer
 */
public class SampleSketchInitializer implements Initializer {

    /**
     * initConfig
     * @param context
     */
    public static void initConfig(Context context) {
        onEvent(new Events.AppConfigChangedEvent(AppConfig.Key.OUT_LOG_2_SDCARD), context);
        onEvent(new Events.AppConfigChangedEvent(AppConfig.Key.LOG_LEVEL), context);
        onEvent(new Events.AppConfigChangedEvent(AppConfig.Key.LOG_TIME), context);
        onEvent(new Events.AppConfigChangedEvent(AppConfig.Key.LOG_REQUEST), context);
        onEvent(new Events.AppConfigChangedEvent(AppConfig.Key.LOG_CACHE), context);
        onEvent(new Events.AppConfigChangedEvent(AppConfig.Key.LOG_ZOOM), context);
        onEvent(new Events.AppConfigChangedEvent(AppConfig.Key.LOG_ZOOM_BLOCK_DISPLAY), context);

        onEvent(new Events.AppConfigChangedEvent(AppConfig.Key.MOBILE_NETWORK_PAUSE_DOWNLOAD), context);
        onEvent(new Events.AppConfigChangedEvent(AppConfig.Key.GLOBAL_LOW_QUALITY_IMAGE), context);
        onEvent(new Events.AppConfigChangedEvent(AppConfig.Key.GLOBAL_IN_PREFER_QUALITY_OVER_SPEED), context);
        onEvent(new Events.AppConfigChangedEvent(AppConfig.Key.GLOBAL_DISABLE_CACHE_IN_DISK), context);
        onEvent(new Events.AppConfigChangedEvent(AppConfig.Key.GLOBAL_DISABLE_BITMAP_POOL), context);
        onEvent(new Events.AppConfigChangedEvent(AppConfig.Key.GLOBAL_DISABLE_CACHE_IN_MEMORY), context);

    }

    /**
     * onEvent
     * @param event
     * @param context
     */
    @Subscribe
    public static void onEvent(Events.AppConfigChangedEvent event, Context context) {
        Configuration configuration = Sketch.with(context).getConfiguration();
        switch (event.key) {
            case OUT_LOG_2_SDCARD :
            case LOG_TIME :
            case LOG_REQUEST :
            case LOG_CACHE :
            case LOG_ZOOM :
                break;

            case LOG_LEVEL :
                AppConfig.setLogLevel(context);
                break;

            case LOG_ZOOM_BLOCK_DISPLAY :
                if (AppConfig.getBoolean(context, AppConfig.Key.LOG_ZOOM_BLOCK_DISPLAY)) {
                    SLog.openType(SLog.TYPE_ZOOM_BLOCK_DISPLAY);
                } else{
                    SLog.closeType(SLog.TYPE_ZOOM_BLOCK_DISPLAY);
                }
                break;

            case MOBILE_NETWORK_PAUSE_DOWNLOAD :
                configuration.setMobileDataPauseDownloadEnabled(AppConfig.getBoolean(context, AppConfig.Key.MOBILE_NETWORK_PAUSE_DOWNLOAD));
                break;

            case GLOBAL_LOW_QUALITY_IMAGE :
                configuration.setLowQualityImageEnabled(AppConfig.getBoolean(context, AppConfig.Key.GLOBAL_LOW_QUALITY_IMAGE));
                break;

            case GLOBAL_IN_PREFER_QUALITY_OVER_SPEED :
                configuration.setInPreferQualityOverSpeedEnabled(AppConfig.getBoolean(context, AppConfig.Key.GLOBAL_IN_PREFER_QUALITY_OVER_SPEED));
                break;

            case GLOBAL_DISABLE_CACHE_IN_DISK :
                boolean cacheInDisk = AppConfig.getBoolean(context, AppConfig.Key.GLOBAL_DISABLE_CACHE_IN_DISK);
                configuration.getDiskCache().setDisabled(cacheInDisk);
                break;

            case GLOBAL_DISABLE_BITMAP_POOL :
                boolean bitmapPool = AppConfig.getBoolean(context, AppConfig.Key.GLOBAL_DISABLE_BITMAP_POOL);
                configuration.getBitmapPool().setDisabled(bitmapPool);
                break;

            case GLOBAL_DISABLE_CACHE_IN_MEMORY :
                boolean cacheInMemory = AppConfig.getBoolean(context, AppConfig.Key.GLOBAL_DISABLE_CACHE_IN_MEMORY);
                configuration.getMemoryCache().setDisabled(cacheInMemory);
                break;
            default:
                break;
        }
    }

    @Override
    public void onInitialize(Context context, Configuration configuration) {
        initConfig(context);
        EventBus.getDefault().register(this);
    }
}